<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="sec"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>View agent ${viewedAgent.username}'s profile</title>
</head>
<body>

	<table>
		<tbody>
			<tr>
				<td>Username</td>
				<td>${viewedAgent.username}</td>
			</tr>
			<tr>
				<td>Firstname</td>
				<td>${viewedAgent.firstname}</td>
			</tr>
			<tr>
				<td>Lastname</td>
				<td>${viewedAgent.lastname}</td>
			</tr>
			<tr>
				<td>Email</td>
				<td>${viewedAgent.email}</td>
			</tr>
			<tr>
				<td>Registration date</td>
				<td>${viewedAgent.creationTime}</td>
			</tr>
			<tr>
				<td>Modification date</td>
				<td>${viewedAgent.modificationTime}</td>
			</tr>
			<tr>
				<td>Birth date</td>
				<td>${viewedAgent.birthDate}</td>
			</tr>
			<tr>
				<td>Gender</td>
				<td>${viewedAgent.gender}</td>
			</tr>
			<tr>
				<td>Folder with CV</td>
				<c:if test="${ not empty viewedAgent.indexedDocumentId }">
					<td>${viewedAgent.folderWithCv}
					<sec:authorize access="hasAnyRole('${ agentRole }')">
						<c:set var="loggedUserId"><sec:authentication property="principal.id"/></c:set>
						<c:if test="${ viewedAgent.id eq loggedUserId }">
							<a href="${pageContext.request.contextPath}/agents/delete-cv">Delete CV</a>
						</c:if>
					</sec:authorize>
					</td>
				</c:if>
				<c:if test="${ empty viewedAgent.indexedDocumentId }">
					<td>-- No uploaded CV --</td>
				</c:if>
			</tr>
		</tbody>
	</table>

	<sec:authorize access="isAuthenticated()">
		
		<sec:authorize ifAnyGranted="${ advertiserRole }">
			<c:set var="contacted">${ requestScope.agentContacted }</c:set>
			<c:if test="${ not contacted }">Contact this agent:</c:if>
			<form:form action="${pageContext.request.contextPath}/agents/view/${viewedAgent.username}/contact" modelAttribute="contactAgentDto" method="POST">
				<form:input path="message" placeholder="Message to agent" disabled="${ contacted }" required="required" />
				<input type="submit" value="Contact" <c:if test="${ contacted }">disabled</c:if> >
			</form:form>
			<c:if test="${ contacted }">You already contacted this agent.</c:if>
		</sec:authorize>
	
		<sec:authorize ifAnyGranted="${ agentRole }">
			<c:set var = "loggedAgentId"><sec:authentication property="principal.id"/></c:set>
			<c:if test="${ loggedAgentId eq viewedAgent.id }">
				<c:if test="${ not empty viewedAgent.contactedByAdvertiser }">
				<table>
					<thead>
						<tr>
							<td>Contacted by following advertisers</td>
						</tr>
					</thead>
					<tbody>
						<c:forEach var="currentAdvertiser" items="${viewedAgent.contactedByAdvertiser}">
							<tr>
								<td><a href="${pageContext.request.contextPath}/advertisers/view/${currentAdvertiser.username}">${currentAdvertiser.username}</a></td>
							</tr>
						</c:forEach>
					</tbody>
				</table>
				</c:if>
				<c:if test="${ empty viewedAgent.contactedByAdvertiser }">
					-- Not contacted by any advertisers, yet --
				</c:if>
			
				<c:if test="${ not empty viewedAgent.applicationsForAdvert }">
				<table>
					<thead>
						<tr>
							<td>Applications for following adverts</td>
						</tr>
					</thead>
					<tbody>
						<c:forEach var="currentAdvert" items="${viewedAgent.applicationsForAdvert}">
							<tr>
								<td><a href="${pageContext.request.contextPath}/adverts/view/${currentAdvert.id}">${currentAdvert.title}</a></td>
							</tr>
						</c:forEach>
					</tbody>
				</table>
				</c:if>
				<c:if test="${ empty viewedAgent.applicationsForAdvert }">
					-- Not applied for any advert, yet --
				</c:if>
			</c:if>
		</sec:authorize>
		
		<sec:authorize ifAnyGranted="${ adminRole }">
		
			<br>
			<a href="${pageContext.request.contextPath}/admins/edit-agent/${ viewedAgent.id }">Edit agent</a>
			
			<br><br>
			Delete user:
			<form:form action="${pageContext.request.contextPath}/admins/delete-agent/" method="POST" commandName="deleteUserDto">
				<form:hidden path="id" value="${ viewedAgent.id }" />
				<form:label path="reason">Reason for deletion</form:label>
				<form:input path="reason" required="required" />
				<br>
				<input type="submit" value="Delete">
			</form:form>
		</sec:authorize>
		
	</sec:authorize>

</body>
</html>